package com.ypf.survey.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.ypf.survey.dao.AnswerDAO;
import com.ypf.survey.dao.OptionDAO;
import com.ypf.survey.dao.QuestionDAO;
import com.ypf.survey.model.Answer;
import com.ypf.survey.model.Option;
import com.ypf.survey.model.Question;
import com.ypf.survey.service.QuestionService;
import com.ypf.survey.service.dto.AnswerDto;
import com.ypf.survey.service.dto.OptionDto;
import com.ypf.survey.service.dto.QuestionDto;

public class QuestionServiceImpl implements QuestionService {

	@Autowired
	private Mapper mapper;
	
	@Autowired
	private QuestionDAO questionDAO;
	
	@Autowired
	private AnswerDAO answerDAO;
	
	@Autowired
	private OptionDAO optionDAO;
	
	@Transactional
	public List<QuestionDto> getQuestions() {
		List<Question> questions = questionDAO.getQuestions();
		return getDtos(questions);
	}
	
	@Override
	public List<AnswerDto> getAnswers() {
		List<Answer> Answers = answerDAO.getAllAnswers();
		return getAnswerDtos(Answers);
	}
	
	@Override
	public List<OptionDto> getOptions() {
		List<Option> options = optionDAO.getAllOptions();
		return getOptionDtos(options);
	}
	
	private List<QuestionDto> getDtos(List<Question> questions) {
		List<QuestionDto> dtos = new ArrayList<QuestionDto>();
		for(Question q : questions) {
			dtos.add(mapper.map(q, QuestionDto.class));
		}
		
		return dtos;
	}
	
	private List<AnswerDto> getAnswerDtos(List<Answer> Answers) {
		List<AnswerDto> dtos = new ArrayList<AnswerDto>();
		for(Answer a : Answers) {
			dtos.add(mapper.map(a, AnswerDto.class));
		}
		
		return dtos;
	}
	
	private List<OptionDto> getOptionDtos(List<Option> options) {
		List<OptionDto> dtos = new ArrayList<OptionDto>();
		for(Option a : options) {
			dtos.add(mapper.map(a, OptionDto.class));
		}
		
		return dtos;
	}
}
